﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using QQ.Util;
using System.Threading;
using RuoTeng.Util;
using System.Windows.Forms;

namespace RTGather.Lib.gather
{

    /// <summary>
    ///cj采集器
    ///
    /// </summary>
   public class fbb_cj
    {
        
        private string countpattern = @"(?isx)total-matched=\u0022(?<count>\d*?)\u0022\s";//总条数 
        //=====================================================================================

        private string areapattern = @"(?isx)<product>(?<content>.*?)</product>";//块

        private string Pidpattern = @"(?isx)<buy-url>(?<content>.*?)</buy-url>";//Pid 

        private string descpattern = @"(?isx)<description>(?<content>.*?)</description>";//描述

        private string titlePattern = @"(?isx)<name>(?<content>.*?)</name>";//标题 

        private string imgPattern = @"(?isx)<image-url>(?<content>.*?)</image-url>";//图片地址

        private readonly string Key = "008d611de7e25bcbbddfc88a316bee6f2fca6ff5244d3348fbde7f90ad419f2de548727b5fab5667f8cd736a1d04b4c72104d54e68106ff62d339bd36fbdc20da1/10640801903eb03a4d2d84c802095c9e9fcba712145933c6ef9ebe7937b86aab936f88ed9bd32097a97691814a8dcee5a1cc9c26248f14b6bfb3476d9937b639";

        private string headname = "Authorization";

        private static string rootpath = Application.StartupPath;//根目录

        private string oldgather = rootpath + "\\已采集产品.txt";

        private List<Product> clist = new List<Product>();

        public void getData(string url,string code,string format)
        {

            string html = Http.GetDataOverride(url, getHead(), null);
                       
            MatchCollection matches = Regex.Matches(html, areapattern);
            clist.Clear();
            
            foreach (Match match in matches)
            {
                string innerurl = "";
                GroupCollection groups = match.Groups;

                if (groups.Count > 0)
                {

                    innerurl = groups["content"].ToString();
                    string pname = getStrFromPattern(titlePattern, innerurl, "content"); 
                    string pid = getStrFromPattern(Pidpattern, innerurl, "content");
                    string desc = getStrFromPattern(descpattern, innerurl, "content");
                    string imgurl = getStrFromPattern(imgPattern, innerurl, "content");
                 
                    Product p = new Product();
                  
                    if (IsRepeat(pid, oldgather) == false)
                    {
                        p.Name = pname;
                        p.Pid = pid;
                        p.imgage = imgurl;
                        p.vcode = makeCode(pid, pname, imgurl);


                        p.desc = desc;
                        if (desc == "没有登记")
                        {
                            p.desc = pname;
                        }  

                        //结束
                        //写入文件
                        writeNew(pid, oldgather);
                        clist.Add(p);
                    } 
                }

            }
        }


        public HashMap<string, string> getHead()
        {

            HashMap<string, string> hm = new HashMap<string, string>();
            hm.Add(this.headname, this.Key);
            return hm;
        }

       /// <summary>
       /// 获取url
       /// </summary>
       /// <param name="keyword"></param>
       /// <param name="pagenum"></param>
       /// <returns></returns>
        public string getUrl(string keyword, string vcode,int pagenum)
        {
            string url = string.Format("https://product-search.api.cj.com/v2/product-search?website-id={0}&keywords={1}&serviceable-area=US&advertiser-ids=joined&page-number={2}", vcode,keyword, pagenum);
            return url;
        }




        public string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Regex.Replace(Htmlstring, @"<script.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style.*?</style>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");

            return Htmlstring;
        }



        /// <summary>
        /// 判断是否重复
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private bool IsRepeat(string url, string filename)
        {
            List<string> list = TextBoxTools.getFileList(filename);
            bool isrepeat = list.Contains(url);
            return isrepeat;
        }


        /// <summary>
        /// 写入新的记录
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="filename"></param>
        private void writeNew(string pid, string filename)
        {
            string old = RTFile.ReadFile(filename);
            string newstring = old + pid + System.Environment.NewLine;
            RTFile.WriteFile(filename, newstring);

        }
         
        public string getStrFromPattern(string pattern,string content,string name)
        {
            Match match = Regex.Match(content, pattern);
            string result="";
            if (match.Success)
            {
                GroupCollection groups = match.Groups;
                result = groups[name].ToString();
            }
            else
            {
                result = "没有登记";
            }
            return result; 
        }


 



        public string makeCode(string pid, string title,string img)
        {

            string result = string.Format("<a href=\"{0}\" target=\"_top\"><img src=\"{1}\" /><br/>{2}</a>",pid,img,title);

            return result;
        }

        /// <summary>
        /// 根据关键词搜索提取条数
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public int getCount(string keyword,string vcode)
        {
            string url = getUrl(keyword,vcode,1);

            string result = Http.GetDataOverride(url, getHead(), null);

            string count = getStrFromPattern(countpattern, result, "count");
            int num = 0;
            if (count != "没有登记")
            {
                num = ParseType.ParseInt(count, 0);
            } 
            return num;
        }

        /// <summary>
        /// 计算页数 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public int getPageCount(int count)
        {

            int PageSize = 50;

            int pagenum = (count / PageSize) + 1;

            return pagenum;

        }


        /// <summary>
        /// 导出 文件excel
        /// </summary>
        /// <param name="filename"></param>
        public void ExcelExport(string filename)
        {
            Excel.Application xls_app = null;
            Excel._Workbook xls_book = null;
            Excel._Worksheet xls_sheet = null;
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            try
            {
                xls_app = new Excel.ApplicationClass();
                xls_app.Visible = false;
                Excel.Workbooks workBooks = (Excel.Workbooks)xls_app.Workbooks;
                if (!System.IO.File.Exists(filename))
                {
                    xls_book = (Excel.Workbook)workBooks.Add(true);
                    Excel.Sheets _sheets = xls_book.Sheets;
                    xls_sheet = (Excel.Worksheet)_sheets[1];
                    int columnIndex = 1;

                    xls_sheet.Cells[1, columnIndex] = "产品编号"; columnIndex++; 
                    xls_sheet.Cells[1, columnIndex] = "产品标题"; columnIndex++; 
                   xls_sheet.Cells[1, columnIndex] = "产品描述"; columnIndex++;  
                    xls_sheet.Cells[1, columnIndex] = "产品代码"; columnIndex++; 
                   
                   
                    xls_book.SaveAs(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                    xls_book.Close(false, miss, miss);

                }
                xls_book = workBooks.Open(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                Excel.Sheets sheets = xls_book.Sheets;
                xls_sheet = (Excel.Worksheet)sheets[1];

                int rowIndex = xls_sheet.UsedRange.Rows.Count == 0 ? 1 : xls_sheet.UsedRange.Rows.Count + 1;

                foreach (Product c in this.clist)
                {

                    int columnIndex = 1;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.Pid;
                    columnIndex++;


                    xls_sheet.Cells[rowIndex, columnIndex] = c.Name;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.desc;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.vcode;
                    columnIndex++;

                    rowIndex++;

                }
                xls_book.Save();
                xls_book.Close(false, miss, miss);
                xls_app.Quit();
            }
            catch(Exception ex) { if (xls_book != null) { xls_book.Save(); xls_book.Close(false, miss, miss); xls_app.Quit(); } }
        }


    }
}
